package com.OrderFood;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

public class GetApplication {

	public static void main(String[] args) throws JMSException {
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.10.44:61616");
		Connection conn = factory.createConnection();
		conn.start();

		Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		Queue queue = session.createQueue("user_msg23");

		MessageConsumer consumer = session.createConsumer(queue);
		TextMessage msg1 = null;
		for (int i=0;i<10;i++){
			System.out.println("fci");
			msg1 = (TextMessage) consumer.receive();
			System.out.println(msg1.getText());
		}
		msg1.acknowledge();

		conn.stop();
		conn.close();
		System.out.println("end.....!");
	}
	
	
	@Test
	public void reciveMessage() throws JMSException
	{
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		JmsTemplate template =(JmsTemplate) context.getBean("jmsTemplate");
		Queue queue = (Queue)context.getBean("queueDestination");
		TextMessage message = (TextMessage) template.receive(queue);
		System.out.println(message.getText());
		context.close();
	}
}
